package com.mvc.service;

import java.lang.reflect.Field;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import com.mvc.entity.ActionInfo;

public class ActionMapping {

	private static HashMap<String,String> actionMap = new HashMap<String,String>();
	
	static{
		actionMap.put("login", "com.mvc.entity.LoginInfo");
		actionMap.put("register", "com.mvc.entity.Register");
	}
	
	public static String getAction(String key) {
		return actionMap.get(key);
	}
	
	public static ActionInfo getActionInfo(String className,HttpServletRequest request) {
		ActionInfo actionInfo = null;
		try {
			Class clazz = Class.forName(className);
			actionInfo = (ActionInfo)clazz.newInstance();
			Field[] declaredFields = clazz.getDeclaredFields();
			for (Field field:declaredFields) {
				field.setAccessible(true);
				field.set(actionInfo, request.getParameter(field.getName()));
				field.setAccessible(false);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return actionInfo; 
	}
	
}
